import pygame
from pygame.sprite import Sprite

SCREEN_WIDTH, SCREEN_HEIGHT = 400, 600


class Music:
    """声音基础类"""

    def __init__(self, filename):
        self.filename = filename
        pygame.mixer.init()
        pygame.mixer.music.load(self.filename)  # 加载音乐

    # 音乐播放
    def play(self):
        pygame.mixer.music.play()


class Baseitem(Sprite):
    """精灵基础类"""

    def __init__(self, color, width, height):
        pygame.sprite.Sprite.__init__(self)


class Tank(Baseitem):
    """坦克基础类"""

    def __init__(self, screen, left, top):
        self.screen = screen
        # 保存加载的图片
        self.images = {
            "U": pygame.image.load("img/p1tankU.gif"),
            "D": pygame.image.load("img/p1tankD.gif"),
            "L": pygame.image.load("img/p1tankL.gif"),
            "R": pygame.image.load("img/p1tankR.gif"),
        }
        self.direction = "U"  # 方向
        self.image = self.images[self.direction]  # 根据图片方向获取图片
        self.rect = self.image.get_rect()  # 根据图片获取区域
        self.rect.left, self.rect.top = left, top
        self.speed = 5  # 移动速度
        self.stop = True  # 坦克移动开关
        self.live = True
        self.OldLeft = self.rect.left
        self.OldTop = self.rect.top

    def displayTank(self):
        # 获取展示对象
        self.image = self.images[self.direction]
        # 调用blit展示
        self.screen.blit(self.image, self.rect)

    def forMove(self):
        """移动"""
        self.OldLeft = self.rect.left
        self.OldTop = self.rect.top
        if self.direction == "L":
            if self.rect.left > 0:
                self.rect.left -= self.speed
        elif self.direction == "U":
            if self.rect.top > 0:
                self.rect.top -= self.speed
        elif self.direction == "D":
            if self.rect.top + self.rect.height < SCREEN_HEIGHT:
                self.rect.top += self.speed
        elif self.direction == "R":
            if self.rect.left + self.rect.height < SCREEN_WIDTH:
                self.rect.left += self.speed

    def shot(self):
        """射击"""
        return Bullet(self)


class MyTank(Tank):
    """我方坦克"""

    def __init__(self, screen, left, top):
        super(MyTank, self).__init__(screen, left, top)
        self.guid = ""
        self.status = "wait"
        self.group = 1  # 阵营
        self.health = {"hp": "100/100", "mp": "100/100"}  # 状态
        self.move = {"ix": 0, "iy": 0, "iz": 0, "speed": 0}  # 位置
        self.look = {"iz": 0, "speed": 0}  # 朝向

    # 移动角度
    def roteMove(self, iz, speed):
        """按角度移动"""
        self.speed = speed
        if self.move["iz"] == 0:
            self.direction = "U"
        elif self.move["iz"] == 90:
            self.direction = "R"
        elif self.move["iz"] == 180:
            self.direction = "D"
        elif self.move["iz"] == 270:
            self.direction = "L"
        self.forMove()
        self.move["ix"] = self.rect.left
        self.move["iy"] = self.rect.top
        self.move["iz"] = iz
        self.move["speed"] = speed


class EnemyTank(Tank):
    """敌方坦克"""

    def __init__(self, screen, left, top, speed):
        super(EnemyTank, self).__init__(screen, left, top)
        self.guid = ""
        self.status = "wait"
        self.group = 0  # 阵营
        self.health = {"hp": "100/100", "mp": "100/100"}  # 状态
        self.move = {"ix": 0, "iy": 0, "iz": 0, "speed": 0}  # 位置
        self.look = {"iz": 0, "speed": 0}  # 朝向

    def setGroup(self, group):
        """设置阵营"""
        self.group = group
        if not group:
            # 替换贴图
            self.images = {
                "U": pygame.image.load("img/enemy1U.gif"),
                "D": pygame.image.load("img/enemy1D.gif"),
                "L": pygame.image.load("img/enemy1L.gif"),
                "R": pygame.image.load("img/enemy1R.gif"),
            }

    def pointMove(self):
        """点位移动"""
        self.rect.left = self.move["ix"]
        self.rect.top = self.move["iy"]
        self.speed = 0
        if self.move["iz"] == 0:
            self.direction = "U"
        elif self.move["iz"] == 90:
            self.direction = "R"
        elif self.move["iz"] == 180:
            self.direction = "D"
        elif self.move["iz"] == 270:
            self.direction = "L"

    def roteMove(self):
        """按角度移动"""
        self.speed = self.move["speed"]
        self.forMove()


class Bullet(Baseitem):
    """子弹类"""

    def __init__(self, tank):
        self.tank = tank
        self.image = pygame.image.load("img/enemymissile.gif")  # 图片加载
        self.direction = tank.direction  # 子弹的方向
        self.rect = self.image.get_rect()  # 获取区域
        if self.direction == "U":  # 子弹的left和top与方向有关
            self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
            self.rect.top = tank.rect.top - self.rect.height
        elif self.direction == "D":
            self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
            self.rect.top = tank.rect.top + tank.rect.height
        elif self.direction == "L":
            self.rect.left = tank.rect.left - self.rect.width / 2 - self.rect.width / 2
            self.rect.top = tank.rect.top + tank.rect.width / 2 - self.rect.width / 2
        elif self.direction == "R":
            self.rect.left = tank.rect.left + tank.rect.width
            self.rect.top = tank.rect.top + tank.rect.width / 2 - self.rect.width / 2
        self.speed = 25  # 子弹的速度
        self.live = True  # 子弹的状态

    # 移动
    def move(self):
        if self.direction == "U":
            if self.rect.top > 0:
                self.rect.top -= self.speed
            else:
                self.live = False  # 修改子弹的状态
        elif self.direction == "R":
            if self.rect.left + self.rect.width < SCREEN_WIDTH:
                self.rect.left += self.speed
            else:
                self.live = False  # 修改子弹的状态
        elif self.direction == "D":
            if self.rect.top + self.rect.height < SCREEN_HEIGHT:
                self.rect.top += self.speed
            else:
                self.live = False  # 修改子弹的状态
        elif self.direction == "L":
            if self.rect.left > 0:
                self.rect.left -= self.speed
            else:
                self.live = False  # 修改子弹的状态

    def displayBullet(self):
        # 将图片加载到窗口
        self.tank.screen.blit(self.image, self.rect)

    # def myBullet_hit_enemyTank(self):
    #     for enemyTank in MainGame.enemyTankList:
    #         if pygame.sprite.collide_rect(enemyTank, self):
    #             enemyTank.live = False
    #             self.live = False
    #             explode = Explode(enemyTank)
    #             MainGame.explodeList.append(explode)

    # def enemyBullet_hit_myTank(self):
    #     if MainGame.my_tank and MainGame.my_tank.live:
    #         if pygame.sprite.collide_rect(MainGame.my_tank, self):
    #             explode = Explode(MainGame.my_tank)  # 爆炸对象
    #             MainGame.explodeList.append(explode)  # 将爆炸对象添加到爆炸列表中
    #             self.live = False  # 修改敌方子弹的状态
    #             MainGame.my_tank.live = False  # 我方坦克的状态

    # def hitWall(self):
    #     for wall in MainGame.WallList:  # 循环遍历墙壁列表
    #         if pygame.sprite.collide_rect(self, wall):  # 检测子弹是否碰撞墙壁
    #             self.live = False  # 修改子弹状态
    #             wall.hp -= 1  # 碰撞后墙壁生命值减少
    #             if wall.hp <= 0:
    #                 wall.live = False
